﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DiagrammInternalStructure.Exceptions;

namespace DiagrammInternalStructure
{
    public class FunctionField:FieldElement
    {
        private FunctionSignature m_signature = null;

        protected FunctionField()
            : base()
        { }

        public FunctionField(string name, ProtectionType protection, ClassElement currentClass, ClassElement type)
            : base(currentClass, protection, name, type)
        {

            CheckOnDuplicates(name, currentClass);
        }
        
        public FunctionField(string name, ProtectionType protection, ClassElement currentClass, ClassElement type, FunctionSignature sign)
            : base(currentClass, protection, name, type)
        {
            m_signature = sign;
        }
        
        public FunctionSignature Signature
        {
            get
            {
                return m_signature;
            }
        }

        private static void CheckOnDuplicates(string name, ClassElement currentClass, FunctionSignature sign = null)
        {
            for (uint i = 0; i < currentClass.FunctionsCount; ++i)
            {
                if (currentClass.GetFunctionByIndex(i).Signature == sign)
                {
                    if (currentClass.GetFunctionByIndex(i).Name == name)
                    {
                        if (sign == null)
                        {
                            throw new EmptyFunctionAlredyFoundException(name, currentClass.Name);
                        }
                        else
                        {
                            throw new FunctionAlreadyFoundException(name, currentClass.Name, sign);
                        }
                    }
                }
            }
        }
    }
}
